package com.logistics.map.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logistics.map.entity.domain.MapRoute;
import com.logistics.map.entity.model.MapRoutePager;
import com.logistics.map.entity.model.MapRouteRow;
import com.logistics.map.entity.model.MapRouteSave;
import com.logistics.map.mapper.MapRouteMapper;
import com.logistics.route.entity.model.RouteRow;
import com.logistics.utils.aspect.annotation.Insert;
import com.logistics.utils.aspect.annotation.Pager;
import com.logistics.utils.bean.ResultPager;
import com.logistics.utils.constants.Message;
import com.logistics.utils.exception.MessageException;
import com.logistics.utils.message.Manager;
import com.logistics.utils.tool.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MapRouteService extends ServiceImpl<MapRouteMapper, MapRoute> {

    @Autowired
    private MapRouteNodesService mapRouteNodesService;

    @Autowired
    private MapRouteEdgesService mapRouteEdgesService;

    /**
     * 分页查询
     * @param param
     * @return
     */
    @Pager
    public ResultPager pager(MapRoutePager param){
        IPage page = new Page(param.getCurrent(), param.getPageSize());
        QueryWrapper<MapRoute> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().like(StringUtils.isNotNull(param.getName()), MapRoute::getName, param.getName());
        queryWrapper.lambda().like(StringUtils.isNotNull(param.getCode()), MapRoute::getCode, param.getCode());
        queryWrapper.lambda().ge(StringUtils.isNotNull(param.getBeginDate()),
                MapRoute::getCreateTime,
                param.getBeginDate())
                .le(StringUtils.isNotNull(param.getEndDate()),
                        MapRoute::getCreateTime,
                        param.getEndDate());
        IPage<MapRoute> result = this.page(page, queryWrapper);
        return new ResultPager(result.getRecords().stream()
                .map(e -> MapRouteRow.build(e)).collect(Collectors.toList()), (int) result.getTotal());
    }

    /**
     * 根据地图ID查找路线
     * @param mapId
     * @return
     */
    public List<MapRouteRow> listByMapId(String mapId){
        return this.list(new LambdaQueryWrapper<MapRoute>().eq(MapRoute::getMapId, mapId))
                .stream().map(e -> MapRouteRow.build(e)).collect(Collectors.toList());
    }

    /**
     * 根据MapId查询路线编码
     * @param mapId
     * @return
     */
    public List<String> routeCodeByMapId(String mapId){
        return this.list(new LambdaQueryWrapper<MapRoute>().eq(MapRoute::getMapId, mapId)).stream().map(e -> e.getCode())
                .collect(Collectors.toList());
    }


    /**
     * 查询详细
     * @param id
     * @return
     * @throws MessageException
     */
    public MapRouteRow details(String id) throws MessageException {
        if(StringUtils.isNull(id)){
            throw new MessageException(Manager.getMessage(Message.MSG1005.getCode()));
        }
        MapRouteRow row = MapRouteRow.build(this.getOne(new LambdaQueryWrapper<MapRoute>().eq(MapRoute::getId, id)));
        return row;
    }

    /**
     * 保存
     * @param save
     */
    @Insert
    @Transactional
    public void save(MapRouteSave save){
        if(StringUtils.isNull(save.getId())){
            this.save(MapRoute.build(save));
            save.getNodes().stream().forEach(e-> mapRouteNodesService.save(e));
            save.getEdges().stream().forEach(e-> mapRouteEdgesService.save(e));
        }else{
            this.update(MapRoute.build(save), new LambdaQueryWrapper<MapRoute>().eq(MapRoute::getId, save.getId()));
        }
    }

    /**
     * 删除
     * @param ids
     * @throws MessageException
     */
    @Transactional
    public void delete(String ids) throws MessageException {
        if(StringUtils.isNull(ids)){
            throw new MessageException(Manager.getMessage(Message.MSG1005.getCode()));
        }
        List<String> routeCode = this.list(new LambdaQueryWrapper<MapRoute>().
                        in(MapRoute::getId , StringUtils.splitToList(ids, ",")))
                .stream().map(e -> e.getCode()).collect(Collectors.toList());
        this.remove(new LambdaQueryWrapper<MapRoute>().in(MapRoute::getId, StringUtils.splitToList(ids, ",")));
        mapRouteNodesService.deleteByRouteCode(routeCode);
        mapRouteEdgesService.deleteByRouteCode(routeCode);
    }
}
